import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;




public class DB_UserVerwalter 
{
	static Vector vUsers;
	
	private static PreparedStatement pstmt = null;
	private static Statement stmt = null;
	
	String connect_String;
	String connect_username;
	String connect_password;
	

	public Connection conn()
	{
		Connection db_verbindung;
		IniReader.readIni("ADV");
		
		connect_String  	= IniReader.UMGEBUNG_CONNECT_STRING;
		connect_username 	= IniReader.UMGEBUNG_USERNAME;
		connect_password 	= IniReader.UMGEBUNG_PASSWORD;
		
		
		
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			db_verbindung = DriverManager.getConnection(connect_String, connect_username, connect_password);
			
		}
		catch (Exception e)
		{
			throw new IllegalArgumentException("Fehler bei Verbindungsaufbau");
		}
		
		return db_verbindung;
	}
	
	public void save(User user)
	{
		
		try
		{
			pstmt = conn().prepareStatement("INSERT INTO t_user VALUES(?,?)");
			
			pstmt.setString(1, user.name);
			pstmt.setString(2, user.password);			
			pstmt.execute();			
			pstmt.close();
				
		}
		catch (SQLException e)
		{
			System.out.println(e.getMessage());
			throw new IllegalArgumentException("Fehler bei DB_Zugriff");
		}
	}
	
	public Vector<User> getUsers()
	{
		vUsers = new Vector();
		
		
		
		try
		{
			String sql_statement = "SELECT * FROM t_user";
			stmt = conn().createStatement();
			ResultSet rset = stmt.executeQuery(sql_statement);
			DB_UserResultSetIterator resultSet = new DB_UserResultSetIterator(rset);
			
//			while (resultSet.hasNext())
//			{
//				vUsers.add(resultSet.next());
//			
//			}
			while (rset.next())
			{
				User akt_User = new User();
				
				akt_User.name 		= rset.getString(1);
				akt_User.password 	= rset.getString(2);
				
				vUsers.add(akt_User);
			}
			
				
		}
		catch (SQLException e)
		{
			throw new IllegalArgumentException("Fehler bei DB_Zugriff");
		}

		
		return vUsers;
	}


}
